import Constants from "../../utils/Constants";
import {queryOrderBenefit, generateOrderBenefit} from "../../services/report/orderbenefit";
const R=require("ramda");
export default {
    namespace:"orderbenefitReport",
    state:{
        page:{},
        list:[]
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, { payload }){
            return {...state, ...payload };
        }
    },
    effects:{
        *queryOrderBenefit({payload},{call,put,select}){
            const res=yield call(queryOrderBenefit,{ ...R.omit(['onSuccess', 'onFail'])(payload) });
            let content=res.data.content;
            if(res.data.resultCode=="ok"){
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: R.isNil(content.data.list)?[]:content.data.list,
                        page: R.isNil(content.data.page) ? Constants.DEFAULTPAGE : content.data.page,
                    }
                });
            }
        },

        *generateOrderBenefit({payload},{call,put,select}){
            payload= R.isNil(payload)?null:payload;
            let beginDate=R.isNil(payload)?'':payload.beginDate;
            let endDate=R.isNil(payload)?'':payload.endDate;
            let contractNo=R.isNil(payload)?'':payload.contractNo;
            let cstName=R.isNil(payload)?'':payload.cstName;
            let isCost=R.isNil(payload)?'':payload.isCost;
            let percent=R.isNil(payload)?'':payload.percent;
            let days=R.isNil(payload)? '': payload.days;
            const res=yield call(generateOrderBenefit,{ beginDate: beginDate, endDate: endDate, contractNo: contractNo, cstName: cstName, isCost: isCost, percent: percent, days: days });
        }
    },

    subscriptions: {
        setup({history, dispatch}) {
            return history.listen(({pathname}) => {
                if(pathname.includes('report/order')) {
                    dispatch({
                        type: 'getOrderList'
                    });
                }
            });
        }
    }


}